﻿namespace Swimj.Core.Utilities
{
    public static class FileHelper
    {
        public static string GetContentType(string extensionName)
        {
            switch (extensionName)
            {
                case ".png":
                    return "image/png";
                default:
                    return "application/octet-stream";
            }
        }

        public static string GetExtensionName(string fileName)
        {
            var lastIndexOf = fileName.LastIndexOf(".", StringComparison.Ordinal);
            var extensionName = fileName.Substring(lastIndexOf);
            return extensionName;
        }

        public static FileInfo CopyFile(string src, string desc)
        {
            if (File.Exists(src))
            {
                FileInfo fileInfo = new FileInfo(src);
                return fileInfo.CopyTo(desc);
            }
            return null;
        }

        public static string CopyFileTo(string src, string descFolder, bool deleteSrc = false)
        {
            if (File.Exists(src))
            {
                FileInfo fileInfo = new FileInfo(src);
                string name = fileInfo.Name;
                string destFileName = descFolder + name;
                FileInfo fileInfo2 = fileInfo.CopyTo(destFileName, overwrite: true);
                if (fileInfo2 != null && fileInfo2.Exists)
                {
                    if (deleteSrc)
                    {
                        fileInfo.Delete();
                    }
                    return fileInfo2.FullName;
                }
            }
            return string.Empty;
        }

        public static void DeleteFile(string filePath)
        {
            if (File.Exists(filePath))
            {
                FileInfo fileInfo = new FileInfo(filePath);
                if (fileInfo.Attributes.ToString().IndexOf("ReadOnly") != -1)
                {
                    fileInfo.Attributes = FileAttributes.Normal;
                }
                File.Delete(filePath);
            }
        }

        public static string GetFileName(string filePath, bool excludeExt = false)
        {
            return GetFileName(filePath, checkExists: true, excludeExt);
        }

        public static string GetFileName(string filePath, bool checkExists, bool excludeExt)
        {
            string empty = string.Empty;
            if (checkExists && !File.Exists(filePath))
            {
                return empty;
            }
            FileInfo fileInfo = new FileInfo(filePath);
            empty = fileInfo.Name;
            if (excludeExt)
            {
                empty = empty.TrimEnd(fileInfo.Extension.ToCharArray());
            }
            return empty;
        }

        public static string GetFileExt(string filePath, bool checkExists = true)
        {
            if (checkExists && !File.Exists(filePath))
            {
                return string.Empty;
            }
            FileInfo fileInfo = new FileInfo(filePath);
            return fileInfo.Extension;
        }
    }
}